<?php

// +----------------------------------------------------------------------
// | 文档控制器
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\cms\admin;

use app\model\cms\Notice as NoticeModel;
use app\model\app\Modular as ModularModel;
use yicmf\builder\Edit;
use yicmf\builder\Table;
use app\controller\admin\Common;
use think\facade\Url;

class Notice extends Common
{

    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * 分类文档列表页
     * @return string
     * @author 微尘 <yicmf@qq.com>
     */
    public function index()
    {
        $map[] = ['status', '>', -2];
        // 显示页面
        $builder = new Table();
        $builder->model(NoticeModel::class)
            ->buttonUpdate()
            ->where($map)->keyId()
//				->keyImageModel('cover_id', '封面')
            ->keyImageModel('cover_id', '封面')
            ->keyTitle()
//            ->keyText('category.name', '分类栏目', false, 200)
//            ->keyText('modular.name', '关联首页', false, 200)
            ->keyUpdateTime()
            ->keyStatus()
            ->actionUpdate('update?id={$id}', '编辑', function ($data) {
                return true;
            })
            ->actionDelete()
            //                ->actionToCheck('check?id={$id}','审核',[0])
//				->actionForbid()
            ->actionRestore();
        return $builder->fetch();
    }


    /**
     * 文档更新页面初始化.
     */
    public function update()
    {
        if ($this->request->isPost()) {
            // 验证数据
            if ($this->request->param('id')) {
                $data = NoticeModel::where('id', $this->request->param('id'))->find();
                $info = $this->request->only(['title', 'category_id', 'cover_id', 'modular_id', 'description', 'content']);
                $data->save($info);
                $this->_returnSuccess('更新成功');
            } else {
                NoticeModel::create($this->request->param());
                $this->_returnSuccess('新增成功');
            }
        } else {
            if ($this->request->has('id', 'get')) {

                $data = NoticeModel::where('id', $this->request->get('id'))->find();
            } else {
                // 创建的默认值
                $data['category_id'] = $this->request->get('category_id');
            }

            // 获取表单字段
            $builder = new Edit();
            $builder->title('公告');
            $builder
//                    ->keyImageModel('cover_id', '封面')
                ->keyRadio('type', '类型', ['普通', '站外链接', '站内文章'])
                ->keyText('title', '标题')
                ->keyImageModel('cover_id','封面')
                ->keyText('relation_id','资讯ID','站内发布文章的ID')
                ->keyText('url','站外链接','公众号链接等')
//                ->keyDateTimeRange('start_time|end_time', '展示时间','',time_format().' - '.time_format('1 year'))

                ->keyDateTimeRange('start_time|end_time', '展示时间','',[time_format(),time_format('1 year')])
                ->keyEditor('content', '内容')
                ->setTrigger('type', 0, '')
                ->setTrigger('type', 1, 'url')
                ->setTrigger('type', 2, 'relation_id')
                ->data($data)
                ->buttonSubmit();
            return $builder->fetch();
        }
    }

    /**
     * 删除消息
     */
    public function delete()
    {
        $message = DocumentModel::where('id', $this->request->param('id'))->find();
        if ($message) {
            $message['status'] = -2;
            $message->save();
            $this->_returnSuccess('删除成功');
        } else {
            $this->_returnSuccess('操作失败');
        }
    }

    /**
     * 文档更新页面初始化.
     */
    //        public function update()
    //        {
    //            if ( $this->request->isPost() ) {
    //                $data = $this->model->find($this->request->param('id',0));
    //                if ($data)
    //                {
    //                    $data['title'] = $this->request->post('title');
    //                    $data['view'] = $this->request->post('view');
    //                    if ($data->save())
    //                    {
    //                        $this->_returnSuccess( '更新成功',true);
    //                    }else{
    //                        $this->_returnError( '更新失败');
    //                    }
    //                }else{
    //                    $this->_returnError( '文档不存在，或者已经删除');
    //                }
    //            } else {
    //                    // 获取详细数据
    //                    $data = $this->model->find($this->request->get('id',0));
    //                    if ( !$data) {
    //                        $this->_returnError( '文档不存在，或者已经删除');
    //                    }
    //
    //                $builder = new Edit();
    //                $builder->title('编辑文档');
    //                $builder->keyText('title','标题','',50)
    //                    ->keyNumber('view','阅读量')
    //                    ->keyNumber('share_coin','分享得绣币')
    //                    ->keyNumber('all_coin','已经获取绣币')
    //                ;
    //                $builder->data($data)
    //                    ->buttonSubmit('', '保存')
    //                    ->buttonClose();
    //                return $builder->fetch();
    //            }
    //        }
}
